<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/18
 * Time: 下午6:09
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Game extends Base
{
    public $game_table = 'game_log';
    public $game_user_table = 'game_user_log';
    public $room_pk_table = 'room_pk';
    public $room_pk_user_log_table = 'room_pk_user_log';
    public $timestamps = false;

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->game_table, $attributes);
    }

    //添加游戏开启记录
    public function createGameLog($data)
    {
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());
        return DB::table($this->game_table)->insertGetId($data);
    }

    //添加游戏用户记录
    public function createGameUser($data)
    {
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());
        return DB::table($this->game_user_table)->insertGetId($data);
    }

    //更新游戏开启记录
    public function updateGameLog($id, $data=[], $where=[])
    {
        $upData = [];
        if(isset($data['mg_id'])) {
            $upData['mg_id'] = $data['mg_id'];
        }
        if(isset($data['battle_start_at'])) {
            $upData['battle_start_at'] = $data['battle_start_at'];
        }
        if(isset($data['battle_end_at'])) {
            $upData['battle_end_at'] = $data['battle_end_at'];
        }
        if(isset($data['battle_duration'])) {
            $upData['battle_duration'] = $data['battle_duration'];
        }
        if(!empty($upData)) {
            DB::table($this->game_table)
                ->where('id', $id)
                ->update($upData);
        }

        return 1;
    }

    //更新游戏用户记录
    public function updateGameUser($data=[], $where=[])
    {
        $upData = [];
        if(isset($data['rank'])) {
            $upData['rank'] = $data['rank'];
        }
        if(isset($data['is_escaped'])) {
            $upData['is_escaped'] = $data['is_escaped'];
        }
        if(isset($data['score'])) {
            $upData['score'] = $data['score'];
        }
        if(isset($data['is_win'])) {
            $upData['is_win'] = $data['is_win'];
        }
        if(isset($data['is_managed'])) {
            $upData['is_managed'] = $data['is_managed'];
        }

        $whereData = [];
        if(isset($where['id'])) {
            $whereData['id'] = $where['id'];
        }
        if(isset($where['game_id'])) {
            $whereData['game_id'] = $where['game_id'];
        }
        if(isset($where['room_id'])) {
            $whereData['room_id'] = $where['room_id'];
        }
        if(isset($where['user_id'])) {
            $whereData['user_id'] = $where['user_id'];
        }

        if(!empty($upData) && !empty($whereData)) {
            DB::table($this->game_user_table)
                ->where($whereData)
                ->update($upData);
        }

        return 1;
    }


    //添加房间团战记录
    public function createRoomPk($data)
    {
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());
        return DB::table($this->room_pk_table)->insertGetId($data);
    }

    //更新房间团战记录
    public function updateRoomPk($id, $data=[], $where=[])
    {
        $upData = [];
        if(isset($data['team_score_1'])) {
            $upData['team_score_1'] = $data['team_score_1'];
        }
        if(isset($data['team_score_2'])) {
            $upData['team_score_2'] = $data['team_score_2'];
        }
        if(isset($data['end_at'])) {
            $upData['end_at'] = $data['end_at'];
        }
        if(isset($data['status'])) {
            $upData['status'] = $data['status'];
        }
        if(isset($data['micro_user_num'])) {
            $upData['micro_user_num'] = $data['micro_user_num'];
        }
        if(!empty($upData)) {
            DB::table($this->room_pk_table)
                ->where('id', $id)
                ->update($upData);
        }

        return 1;
    }

    //累计房间团战记录
    public function incrRoomPk($id, $field, $num=0)
    {
        if(empty($field) || empty($num)) {
            return 0;
        }

        return DB::table($this->room_pk_table)
            ->where('id', $id)
            ->increment($field, $num);
    }

    //添加房间团战用户记录
    public function createRoomPkUserLog($data)
    {
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());
        return DB::table($this->room_pk_user_log_table)->insertGetId($data);
    }



    //获取游戏开启记录
    public function getGameLog($id)
    {
        $data = DB::table($this->game_table)
            ->where('id', $id)
            ->first();
        $data = self::objToArray($data);

        return $data;
    }

    //获取游戏开启记录
    public function getGameLogByMgId($mgId)
    {
        $data = DB::table($this->game_table)
            ->where('mg_id', $mgId)
            ->first();
        $data = self::objToArray($data);

        return $data;
    }

    //获取房间团战记录
    public function getRoomPk($id)
    {
        $data = DB::table($this->room_pk_table)
            ->where('id', $id)
            ->first();
        $data = self::objToArray($data);

        return $data;
    }





}
